package com.synnex.mobile.servier.web.survey.controller.admin;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.generic.web.view.RedirectView;
import com.synnex.mobile.servier.core.survey.bo.QuestionnaireBo;
import com.synnex.mobile.servier.core.survey.bo.SurveyBo;
import com.synnex.mobile.servier.web.survey.controller.AbstractSurveyController;
import com.synnex.mobile.servier.web.survey.form.QuestionnaireForm;
import com.synnex.mobile.servier.web.survey.form.SurveyForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-9
 * To change this template use File | Settings | File Templates.
 */
@LogClass(name = "admin survey")
@RequestMapping(url = "/admin/survey")
public class AdminSurveyController extends AbstractSurveyController {

    @RequestMapping(url = "/listQuestionnaire.do")
    @LogMethod(name = "list questionnaire")
    public ModelAndView getQuestionnaireList(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map model = super.getQuestionnaires(request);

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/viewQuestionnaire.do")
    @LogMethod(name = "jump to  questionnaire page")
    public ModelAndView viewQuestionnaire(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map model = super.getQuestionnaires(request);
            String id = request.getParameter("id");
            QuestionnaireBo bo = questionService.getQuestionById(Integer.parseInt(id));
            model.put("data", bo);

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/editQuestionnaire.do", formCommand = "viewQuestionnaire")
    @LogMethod(name = "edit questionnaire")
    public ModelAndView editQuestionnaire(HttpServletRequest request, HttpServletResponse response, QuestionnaireForm form) {

        try {
            Map model = super.getQuestionnaires(request);

            QuestionnaireBo bo = ObjectUtil.copyProperties(form, QuestionnaireBo.class);
            questionService.editQuestionnaire(bo);
            return new ModelAndView(new RedirectView("/admin/survey/listQuestionnaire.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }


    @RequestMapping(url = "/addQuestionnaire.do")
    @LogMethod(name = "jump to add Questionnaire page")
    public ModelAndView addQuestionnaire(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map model = super.getQuestionnaires(request);

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/addQuestionnaireAction.do", formCommand = "addQuestionnaire")
    @LogMethod(name = "add QuestionnaireAction ")
    public ModelAndView addQuestionnaireAction(HttpServletRequest request, HttpServletResponse response, QuestionnaireForm form) {
        Map<String, Object> model = new HashMap<String, Object>();
        try {
            QuestionnaireBo bo = ObjectUtil.copyProperties(form, QuestionnaireBo.class);
            questionService.addQuestionnaire(bo);

            return new ModelAndView(new RedirectView("/admin/survey/listQuestionnaire.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/deleteQuestionnaire.do")
    @LogMethod(name = "delete Questionnaire ")
    public ModelAndView deleteQuestionnaire(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> model = new HashMap<String, Object>();

            String id = request.getParameter("id");
            questionService.deleteQuestionnaire(Integer.parseInt(id));

            return new ModelAndView(new RedirectView("/admin/survey/listQuestionnaire.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }


///////////////////////////////////////////////


    @RequestMapping(url = "/listSurvey.do")
    @LogMethod(name = "list survey")
    public ModelAndView getSurveyList(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map model = super.getSurveys(request);
            String qid = request.getParameter("qid");

            List<QuestionnaireBo> Questionnaires = (List<QuestionnaireBo>) super.getQuestionnaires(request).get("results");
            Integer qCount = (Integer) super.getQuestionnaires(request).get("totalCount");
            model.put("qCount", qCount);
            model.put("questions", Questionnaires);
            model.put("qid", qid);

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/addSurvey.do")
    @LogMethod(name = "jump to add survey page")
    public ModelAndView addSurvey(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map model = new HashMap<String, Object>();
            List<QuestionnaireBo> Questionnaires = (List<QuestionnaireBo>) super.getQuestionnaires(request).get("results");
            Integer qCount = (Integer) super.getQuestionnaires(request).get("totalCount");
            model.put("qCount", qCount);
            model.put("questions", Questionnaires);

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/addSurveyAction.do", formCommand = "addSurvey")
    @LogMethod(name = "add addSurvey Action ")
    public ModelAndView addSurveyAction(HttpServletRequest request, HttpServletResponse response, SurveyForm form) {
        Map<String, Object> model = new HashMap<String, Object>();
        try {
            SurveyBo bo = ObjectUtil.copyProperties(form, SurveyBo.class);
            surveyService.addSurvey(bo);

            model.put("qid", form.getQuestionnaireId());

            return new ModelAndView(new RedirectView("/admin/survey/listSurvey.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/viewSurvey.do")
    @LogMethod(name = "jump to  survey page")
    public ModelAndView viewSurvey(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> model = new HashMap<String, Object>();
            List<QuestionnaireBo> Questionnaires = (List<QuestionnaireBo>) super.getQuestionnaires(request).get("results");
            Integer qCount = (Integer) super.getQuestionnaires(request).get("totalCount");
            model.put("qCount", qCount);
            model.put("questions", Questionnaires);
            model.put("qid", request.getParameter("qid"));


            String id = request.getParameter("id");
            SurveyBo bo = surveyService.getSurveyById(Integer.parseInt(id));
            model.put("data", bo);
            model.put("aItemSize", bo.getAnswerItems().size());

            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/editSurvey.do", formCommand = "viewSurvey")
    @LogMethod(name = "edit survey")
    public ModelAndView editSurvey(HttpServletRequest request, HttpServletResponse response, SurveyForm form) {

        Map<String, Object> model = new HashMap<String, Object>();
        try {
            SurveyBo bo = ObjectUtil.copyProperties(form, SurveyBo.class);
            surveyService.editSurvey(bo);

            model.put("qid", form.getQuestionnaireId());

            return new ModelAndView(new RedirectView("/admin/survey/listSurvey.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/deleteSurvey.do")
    @LogMethod(name = "delete survey ")
    public ModelAndView deleteSurvey(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> model = new HashMap<String, Object>();

            String id = request.getParameter("id");
            String qid = request.getParameter("qid");
            model.put("qid", qid);
            surveyService.deleteSurveyById(Integer.parseInt(id));

            return new ModelAndView(new RedirectView("/admin/survey/listSurvey.do"), model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url = "/viewSurveyResult.do")
    @LogMethod(name = "view survey result ")
    public ModelAndView viewSurveyResult(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> model = new HashMap<String, Object>();

            String id = request.getParameter("id");
            List<SurveyBo> surveyBoList = surveyService.listSACount(Integer.parseInt(id));
            QuestionnaireBo questionnaireBo = questionService.getQuestionById(Integer.parseInt(id));
            Integer totalSurvey = surveyService.countTotalForQuestionnaire(Integer.parseInt(id));

            model.put("results",surveyBoList);
            model.put("question",questionnaireBo);
            model.put("count",surveyBoList.size());
            model.put("doSurvey",totalSurvey);
            return new ModelAndView(model);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }
}
